<?php

/*
	This file is part of Mandragon.

    Mandragon is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Mandragon is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Mandragon.  If not, see <http://www.gnu.org/licenses/>.
*/

//TODO (mandragon): add region_id

include_once('sys/InputChecker.inc');

//TODO: add_address and edit_address don't have a corresponding table in the database.
//		Check whether they can be removed together with the location-page.
class LocationManager {

	private $checker;

	public function __construct() {
		$this->checker = new InputChecker();
	}

	private function check_address($address) {
		$ok = $this->checker->check_exists($address['street'], "straat") && 
			  $this->checker->check_exists($address['street_number'], "straatnr") && 
			  $this->checker->check_exists($address['zip'], "postcode") && 
			  $this->checker->check_exists($address['city'], "woonplaats") && 
			  $this->checker->check_tags($address['street'], "straat") && 
			  $this->checker->check_chars($address['street_number'], "straatnr") && 
			  $this->checker->check_chars($address['zip'], "postcode") && 
			  $this->checker->check_tags($address['city'], "woonplaats");	
		return $ok;
	}

	public function add_address($address) {
		
		if ($this->check_address($address)) {
			$sql = db_query_insert(array('street', 'street_number', 'zip', 'city'), array(db_sqlize($address['street']), 
									db_sqlize($address['street_number']), db_sqlize($address['zip']), 
									db_sqlize($address['city'])), "ADDRESS");
			db_do_query($page, $sql);
			return db_insert_id();
		}
		
		return 0;
	}

	public function edit_address($address) {
		if ($this->check_address($address)) {
			$sql = db_query_update(array('street', 'street_number', 'zip', 'city'), array(db_sqlize($address['street']), 
									db_sqlize($address['street_number']), db_sqlize($address['zip']), db_sqlize($address['city'])), 
									"ADDRESS", "address_id = {$_POST['address_id']}");
			db_do_query($page, $sql);
		}
	}

	public function fetch_address($address_id) {
		$sql = db_query_select(array(array('*')), array('ADDRESS'), "address_id = $address_id");
		$sqlresult = db_do_query($page, $sql);
		return db_fetch_array($sqlresult);
	}

	public function fetch_location($location_id) {
		$sql = db_query_select(array(array('*')), array('ACTIVITY_LOCATION'), "activity_location_id = $location_id");
		$sqlresult = db_do_query($page, $sql);
		return db_fetch_array($sqlresult);
	}

	public function list_address() {
		$sql = db_query_select(array(array('*')), array('ADDRESS'), 1, "city ASC, street ASC");
		$sqlresult = db_do_query($page, $sql);
		$ks = array();
		$vs = array();
		while ($r = db_fetch_array($sqlresult)) {
			array_unshift($ks, $r['address_id']);
			array_unshift($vs, "{$r['zip']} {$r['city']}, {$r['street']} {$r['street_number']}");
		}
		return array('keys' => $ks, 'values' => $vs);
	}

	public function form_address($default=array()) {
		$inputs = array();
		$creator = new FormCreator();
		if ($default['address_id']) {
			array_unshift($inputs, $creator->input_hidden("address_id", $default['address_id']));
		}
		array_unshift($inputs, $creator->input_field("Straat", "street", $default['street'], 40, 90));
		array_unshift($inputs, $creator->input_field("Nr", "street_number", $default['street_number'], 5, 10));
		array_unshift($inputs, $creator->input_field("Postcode", "zip", $default['zip'], 7, 10));
		array_unshift($inputs, $creator->input_field("Woonplaats", "city", $default['city'], 30, 55));
		return $creator->create("addressform", $inputs);
	}
}

?>